#pragma once

#include "noncopyable.h"
#include "timestamp.h"

#include <unordered_map>
#include <vector>

class Channel;
class EventLoop;

class Poller : noncopyable {
  public:
    using ChannelList = std::vector<Channel *>;

    Poller(EventLoop *loop);
    virtual ~Poller() = default;

    // 给所有IO复用保留统一接口
    virtual Timestamp poll(int timeout_ms, ChannelList *active_channels) = 0;
    virtual void update_channel(Channel *channel) = 0;
    virtual void remove_channel(Channel *channel) = 0;

    // 判断参数Channel是否在当前Poller中
    bool has_channel(Channel *channel) const;

    // 这个的实现需要结合具体类，需要子类的依赖，所以单独一个文件实现，通过该接口获得默认的I/O多路复用具体实现
    static Poller *new_default_poller(EventLoop *loop);

  protected:
    // 键是sockfd,值是对应的channel类型
    using ChannelMap = std::unordered_map<int, Channel *>;
    ChannelMap channels_;

  private:
    EventLoop *owner_loop_; //
};